//
//  SqDetailListViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/8/3.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "HomeFinshtViewController.h"
#import "SqFinshViewCell.h"
#import "TypeCell.h"
#import <TZImagePickerController/TZImagePickerController.h>
@interface HomeFinshtViewController ()<TZImagePickerControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *shenqLabel;
@property (weak, nonatomic) IBOutlet UILabel *bumenLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UILabel *spTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *spBumenLabel;
@property (weak, nonatomic) IBOutlet UILabel *spContnetLabel;

@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (nonatomic ,strong) AppealFinshModel *model;
@property (nonatomic, copy) NSString *auth;
@property (nonatomic ,copy) NSString *type;
@property (weak, nonatomic) IBOutlet UILabel *pingLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentPLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UILabel *imageLabel;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UIView *imageSeleView;

@end

@implementation HomeFinshtViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"诉求详情";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self getData];
    UICollectionViewFlowLayout *flow1 = [[UICollectionViewFlowLayout alloc] init];
          [flow1 setScrollDirection:UICollectionViewScrollDirectionHorizontal];//竖直滑动
          flow1.itemSize = CGSizeMake(70, 70);
          self.collectionView.delegate = self;
            flow1.sectionInset = UIEdgeInsetsMake(1.0, 1.0, 1.0, 1.0);
          self.collectionView.dataSource = self;
          self.collectionView.showsHorizontalScrollIndicator = NO;
          self.collectionView.showsVerticalScrollIndicator = NO;
          self.collectionView.collectionViewLayout = flow1;
          [self.collectionView registerNib:[UINib nibWithNibName:@"TypeCell" bundle:nil] forCellWithReuseIdentifier:@"TypeCell6"];//注册cel
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //    return self.treeList.count;
    return self.model.appeal.more.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeCell6" forIndexPath:indexPath];
    PhotosModel *model = self.model.appeal.more.photos[indexPath.row];
    
    //    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:@"%@%@",BaseImgUrl,model.url]];
    
    NSString * str1 = @"\\";
    NSString  *imageUrl = [model.url stringByReplacingOccurrencesOfString:str1 withString:@"/"];
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseImgUrl,imageUrl]]];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //      PhotosModel *model = self.model.more.photos[indexPath.row];
    [self selectedIndex:indexPath.row withImageURLs:self.model.appeal.more.photos];
}

- (void)selectedIndex:(NSInteger)index withImageURLs:(NSArray *)urlsArray{
    NSMutableArray *datas = [NSMutableArray array];

    for (PhotosModel *model in urlsArray) {
        YBIBImageData *data = [YBIBImageData new];
        NSString * str1 = @"\\";
        NSString  *imageUrl = [model.url stringByReplacingOccurrencesOfString:str1 withString:@"/"];
        data.imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseImgUrl,imageUrl]];
        [datas addObject:data];
    }
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = index;
    [browser show];
}

#pragma mark - Table view data source




//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr{
    long long time=[timeStr longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
    
}

-(void)getData{
    
    if ([[TokenManager getLeader_main] isEqualToString:@"1"]) {
        if ([[TokenManager getIsheadOffice] isEqualToString:@"1"]) {
            self.auth = @"5";
        }else{
            self.auth = @"2";
        }
    }else if ([[TokenManager getSection_normal]isEqualToString:@"1"]){
        
        if ([[TokenManager getIsheadOffice]isEqualToString:@"1"]) {
            self.auth = @"6";
        }else{
            self.auth = @"3";
        }
    }
    WS(weakSelf);
    [[ApiManager ShardInstance]staffAppealDetailID:self.appealID  resultBlock:^(NSDictionary *data, NSError *error) {
        
        
        self.model = [AppealFinshModel mj_objectWithKeyValues:data[@"rs"]];
        
        
        weakSelf.nameLabel.text = [NSString stringWithFormat:@"%@提交的%@",self.model.appeal.real_name,self.model.appeal.type_name];
        weakSelf.codeLabel.text = self.model.appeal.appeal_code;
        weakSelf.shenqLabel.text = self.model.appeal.real_name;
        weakSelf.bumenLabel.text = self.model.appeal.section_name;
        weakSelf.timeLabel.text = [self ConvertStrToTime:self.model.appeal.create_time];
        weakSelf.typeLabel.text = self.model.appeal.type_name;
        weakSelf.contentLabel.text = self.model.appeal.content;
        
        weakSelf.spTimeLabel.text = [self ConvertStrToTime:self.model.result.create_time];
        weakSelf.spBumenLabel.text = self.model.result.section_name;
        weakSelf.spContnetLabel.text = self.model.result.content;
        weakSelf.pingLabel.text = self.model.appraise.content;
        weakSelf.contentPLabel.text = self.model.appraise.content;
        if (weakSelf.model.appeal.more.photos.count > 0) {
            weakSelf.headView.height = 240;
            weakSelf.imageLabel.hidden = NO;
            weakSelf.imageSeleView.hidden = NO;
        }else{
            weakSelf.headView.height = 155;
            weakSelf.imageLabel.hidden = YES;
            weakSelf.imageSeleView.hidden = YES;
        }
        
        [self.collectionView reloadData];
        
        [self.tableView reloadData];
        
        
    }];
    
}

@end
